package chapterSystem.server.builder;

import chapterSystem.client.model.enums.UnitType;
import chapterSystem.client.model.entities.*;
import chapterSystem.client.model.entitiesInt.*;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * User: Hasp
 * Date: 29.03.12
 * Time: 22:20
 */
public class EntityBuilder {
    static final private String BROTHER_ID_FIELD = "brother.brother_id";
    static final private String BROTHER_NAME_FIELD = "brother.name";
    static final private String BROTHER_UNIT_ID_FIELD = "brother.unit_id";
    static final private String RECEIVER_NAME_FIELD = "receiver.name";
    static final private String BROTHER_RANK_BATTLE_FIELD = "rank_battle";
    static final private String BROTHER_RANK_WORK_FIELD = "rank_work";
    static final private String BROTHER_POINTS = "points";

    static final private String ORDER_ID_FIELD = "order_id";
    static final private String ORDER_AUTHOR_FIELD = "author_id";
    static final private String ORDER_RECEIVER_FIELD = "receiver_id";
    static final private String ORDER_TEXT = "order_text";
    static final private String ORDER_CREATION_DATE = "battle_order.creation_date";
    static final private String ORDER_EXECUTION_DATE_FIELD = "order_execution.creation_date";
    static final private String ORDER_EXECUTION_REPORT_FIELD = "brief_report";

    static final private String REPORT_ID_FIELD = "report_id";
    static final private String REPORT_AUTHOR_FIELD = "author_id";
    static final private String REPORT_EVENT_FIELD = "event_id";
    static final private String REPORT_DATE_FIELD = "creation_date";
    static final private String REPORT_TEXT = "report_text";

    static final private String EVENT_ID_FIELD = "event_id";
    static final private String EVENT_TITLE_FIELD = "title";
    static final private String EVENT_DESCRIPTION_FIELD = "description";
    static final private String EVENT_DATE_FIELD = "chapter_event.creation_date";

    static final private String UNIT_ID_FIELD = "unit.unit_id";
    static final private String UNIT_NAME = "unit.name";
    static final private String UNIT_COMMANDER_ID_FIELD = "unit.commander_id";
    static final private String UNIT_BASE_OUTPOST_FIELD = "unit.base_outpost";
    static final private String UNIT_LOCATION_FIELD = "unit.last_location";
    static final private String UNIT_TYPE_FIELD = "unit.unit_type_id";
    static final private String UNIT_PARENT_ID_FIELD = "unit.parent_unit_id";
    static final private String UNIT_SUPPLIER_FIELD = "unit.supplier_provider_id";


    public static ChapterEvent getEventEntity(ResultSet rs) throws SQLException {
        return new ChapterEventEntity(rs.getInt(EVENT_ID_FIELD), rs.getString(EVENT_TITLE_FIELD),
                rs.getString(EVENT_DESCRIPTION_FIELD), rs.getDate(EVENT_DATE_FIELD));
    }

    //    (int id, int unitId, int rankBattle, String name, int rankWork, int points)
    public static Brother getBrotherEntity(ResultSet rs) throws SQLException {
        return new BrotherEntity(rs.getInt(BROTHER_ID_FIELD), rs.getInt(BROTHER_UNIT_ID_FIELD),
                rs.getInt(BROTHER_RANK_BATTLE_FIELD), rs.getString(BROTHER_NAME_FIELD),
                rs.getInt(BROTHER_RANK_WORK_FIELD), rs.getInt(BROTHER_POINTS));
    }

    public static Order getOrderEntity(ResultSet rs) throws SQLException {
        return new OrderEntity(rs.getDate(ORDER_CREATION_DATE), rs.getString(ORDER_TEXT), rs.getInt(ORDER_RECEIVER_FIELD),
                rs.getInt(ORDER_AUTHOR_FIELD), rs.getInt(ORDER_ID_FIELD), rs.getDate(ORDER_EXECUTION_DATE_FIELD),
                rs.getString(ORDER_EXECUTION_REPORT_FIELD), rs.getString(ORDER_EXECUTION_REPORT_FIELD) != null,
                rs.getString(BROTHER_NAME_FIELD), rs.getString(RECEIVER_NAME_FIELD));
    }

    public static Report getReportEntity(ResultSet rs) throws SQLException {
        return new ReportEntity(rs.getString(REPORT_TEXT), rs.getInt(REPORT_EVENT_FIELD), rs.getInt(REPORT_AUTHOR_FIELD),
                rs.getInt(REPORT_ID_FIELD), rs.getDate(REPORT_DATE_FIELD));
    }

    //    (final int id, final String name, final Brother commander, final String baseOutpost,
//                      final String location, final UnitType type, final int parentId)
    public static Unit getUnitEntity(ResultSet rs) throws SQLException {
        final Brother commander = getBrotherEntity(rs);
        final UnitType type = UnitType.byCode(rs.getInt(UNIT_TYPE_FIELD));
        return new UnitEntity(rs.getInt(UNIT_ID_FIELD), rs.getString(UNIT_NAME), commander, rs.getString(UNIT_BASE_OUTPOST_FIELD),
                rs.getString(UNIT_LOCATION_FIELD), type, rs.getInt(UNIT_PARENT_ID_FIELD));
    }

}
